import React, { Component } from 'react';
import { Modal, Form, message, Radio, Select } from 'antd';

const FormItem = Form.Item;
const RadioGroup = Radio.Group;
const { Option } = Select;

export default class OneKeyStart extends Component {
  state = {};

  handleSet = () => {
    this.props.form.validateFields((err, values) => {
      if (!err) {
        const { dispatch } = this.props;
        const { operation, cityId, devScope, setMod } = values;
        if (operation === 0) {
          dispatch({
            type: 'dev/oneKeyOpenScr',
            payload: { cityId, devScope, setMod },
            callback: () => {
              message.success('设置一键开屏成功.');
            },
          });
        } else {
          dispatch({
            type: 'dev/oneKeyCloseScr',
            payload: { cityId, devScope, setMod },
            callback: () => {
              message.success('设置一键关屏成功.');
            },
          });
        }

        this.props.handleModalVisible(false, 'oneKeyStart');
      }
    });
  };

  render() {
    const { modalVisible, form, handleModalVisible } = this.props;
    return (
      <Modal
        title="设置一键开屏"
        visible={modalVisible}
        onOk={this.handleSet}
        onCancel={() => handleModalVisible(false, 'oneKeyStart')}
      >
        <Form>
          <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="选择操作">
            {form.getFieldDecorator('operation', {
              initialValue: 0,
              rules: [{ required: true, message: '请选择屏幕开关...' }],
            })(
              <RadioGroup>
                <Radio value={0}>开</Radio>
                <Radio value={1}>关</Radio>
              </RadioGroup>
            )}
          </FormItem>
          <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="选择范围">
            {form.getFieldDecorator('devScope', {
              initialValue: 0,
            })(
              <RadioGroup>
                <Radio value={0}>绑定设备</Radio>
                <Radio value={1}>所有设备</Radio>
              </RadioGroup>
            )}
          </FormItem>
          <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="选择模式">
            {form.getFieldDecorator('setMod', {
              initialValue: 0,
            })(
              <RadioGroup>
                <Radio value={0}>批量设置</Radio>
                <Radio value={1}>智能设置</Radio>
              </RadioGroup>
            )}
          </FormItem>
          <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="选择城市">
            {form.getFieldDecorator('cityId', {
              rules: [{ required: true, message: '请选择城市...' }],
            })(
              <Select allowClear="true" placeholder="请选择" style={{ width: '100%' }}>
                {this.props.city.map(item => (
                  <Option value={item.code} key={item.code}>
                    {item.name}
                  </Option>
                ))}
              </Select>
            )}
          </FormItem>
        </Form>
      </Modal>
    );
  }
}
